/*
 * Created on 14/11/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.mushroomdb.transaction;

import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.query.Query;
import org.mushroomdb.service.ServiceManager;

/**
 * @author Tomas
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TransactionalQuery extends Query  {
    short type;
    
    public TransactionalQuery(String query){
        
        String query2 = query.trim();
        if (("COMMIT ;").equalsIgnoreCase(query2)) {
            this.type=0;
            
        }else if(("ROLLBACK ;").equalsIgnoreCase(query2)){
            this.type=1;
        }
        else if(query2.toUpperCase().equalsIgnoreCase("BEGIN TRANSACTION ;")){
            this.type=2;
        }
    }

   
    public Object execute(EvaluationEngine evaluationEngine) {
        TransactionService service = (TransactionService) ServiceManager.getInstance().getService(TransactionService.class);
        
        if (this.type==0){
            service.commitTransaction();
            return new Boolean(true);
        }else if (this.type==1){
            service.rollBackTransaction();
            return new Boolean(true);
        }else if (this.type==2){
            service.beginTransaction();
            return new Boolean(true);
        }
        return new Boolean(false);
    }
    
    

}
